require "util"
require "logUtil"

-- 关闭关卡界面
function gq_ui_GB()
    x, y = findMultiColorInRegionFuzzy(0xfed738, "-33|-3|0x9a140c,-9|-32|0x9c160b,-116|43|0x321500,-124|78|0x2a3043,-191|114|0xeabfab,-466|152|0x060000,-432|130|0xfef20f,-438|196|0x702421,-471|227|0x6e2320,-465|111|0xfef10c,-436|109|0x050000,-206|54|0x5b6385,-242|45|0x321500", 85, 492, 85, 1079, 464, 0, 0)
    if x == -1 then
        x, y = findMultiColorInRegionFuzzy(0xfecf2c,"8|4|0xfec820,-15|11|0xfebd0e,3|33|0x940f0a,31|-5|0xa31b0c,-35|-5|0x96110b,-189|109|0xeabfab,-433|126|0xfef33d,-467|221|0x6e2320,-362|175|0x282e3f,-149|121|0x161920,-393|123|0x161921", 85, 520, 118, 1068, 420, 0, 0)
    end
    if x > -1 then
        click(x, y)
        logger("关卡界面关闭", LOG_DEBUG)
        return true
    end


end


-- 关卡界面
function gq_ui()
    x, y = findMultiColorInRegionFuzzy(0xfed738, "-33|-3|0x9a140c,-9|-32|0x9c160b,-116|43|0x321500,-124|78|0x2a3043,-191|114|0xeabfab,-466|152|0x060000,-432|130|0xfef20f,-438|196|0x702421,-471|227|0x6e2320,-465|111|0xfef10c,-436|109|0x050000,-206|54|0x5b6385,-242|45|0x321500", 85, 492, 85, 1079, 464, 0, 0)
    if x == -1 then
        x, y = findMultiColorInRegionFuzzy(0xfecf2c,"8|4|0xfec820,-15|11|0xfebd0e,3|33|0x940f0a,31|-5|0xa31b0c,-35|-5|0x96110b,-189|109|0xeabfab,-433|126|0xfef33d,-467|221|0x6e2320,-362|175|0x282e3f,-149|121|0x161920,-393|123|0x161921", 85, 520, 118, 1068, 420, 0, 0)
    end

    if x > -1 then
        logger("关卡界面", LOG_DEBUG)
        return true
    end
end



-- 查找可以战斗的关卡
function gq_findGQ_boss()

    x, y = findMultiColorInRegionFuzzy(0xfedd00, "29|1|0xfed90f,46|7|0xfea710,67|5|0xfcb100,86|-33|0xfef782,-25|-35|0xfef784,-22|-53|0xfef791,14|13|0xaf0707,44|-5|0x580000,23|0|0x580000", 80, 20, 466, 1056, 923, 0, 0)
    if x == -1 then
        x, y = findMultiColorInRegionFuzzy(0xfeb60b, "-27|-6|0xfde300,17|3|0xfe9a05,33|4|0xfe9001,-4|-4|0x580000,3|18|0x170e0d,2|32|0xfe8105,28|-1|0x580000,8|-2|0x580000,-4|-5|0x580000,37|-11|0x570000,40|-14|0xfaec00,2|-4|0xfed600,9|-2|0x580000,16|4|0xfe8f00", 85, 23, 461, 1079, 682, 0, 0)

    end
    if x > -1 then

        click(x, y)
        return true
    end

end

-- 查找可以战斗的关卡
function gq_findGQ()

    x, y = findMultiColorInRegionFuzzy(0xfef7a5, "-55|55|0xfef782,57|50|0xfef686,0|105|0x0c0602,0|116|0x373833,0|125|0xfe8105,-11|113|0xfe9919,11|116|0xfe9010", 80, 25, 466, 1050, 1466, 0, 0)

    if (x == -1) then
        x, y = findMultiColorInRegionFuzzy(0x1f1d1a, "-48|19|0xfef797,55|41|0xfef787,47|74|0xe7d65e,4|5|0x060301,-2|98|0x0e0906,-2|120|0x773f0c,0|-6|0xe8e0a0", 80, 55, 473, 1049, 1515, 0, 0)
    end


    if (x == -1) then
        x, y = findMultiColorInRegionFuzzy(0xc9c484, "-27|11|0x8c885e,-54|50|0x545132,58|58|0xfef780,0|125|0xc96807,1|107|0x0c0705,-16|111|0xaa6b17,17|113|0xe28917", 80, 0, 0, 1079, 1919, 0, 0)
    end

    if (x == -1) then
        x, y = findMultiColorInRegionFuzzy(0x120a04, "-15|9|0xd98b1e,16|11|0xe88f19,57|-48|0xfef784,-54|-50|0xaaa65d,2|-101|0xf8f1a1,1|-2|0x110903", 80, 47, 497, 1035, 1498, 0, 0)

    end
    if x > -1 then

        click(x, y)
        return true
    end

end

--  战斗的关卡-点击继续
function gq_guanQia_JiXu()

    x, y = findMultiColorInRegionFuzzy(0x5b6385, "60|0|0x5b6385,1|32|0x030505,74|31|0x010203,107|27|0x010204,97|20|0x010202,70|15|0xefce76,21|16|0xefce76,-15|16|0xefce76,978|2|0x5b6385,968|57|0x000202", 80, 0, 0, 1079, 1919, 0, 0)
    if x > -1 then
        click(x, y)
        return true
    end
end

--  战斗的关卡-关闭
function gq_guanQia_GB()

    x, y = findMultiColorInRegionFuzzy(0xfee34b, "19|-22|0xfee34b,11|10|0xfee34b,-23|15|0xfee34b,-11|-18|0xfee34b,2|-34|0x1e2b43,42|-6|0x1e2b43,-4|26|0x1e2b43,-27|-7|0x1e2b43", 80, 863, 163, 1079, 336, 0, 0)
    if x > -1 then
        click(x, y)
        logger("关卡-关闭", LOG_DEBUG)
        return true
    end
end



-- boss_攻击
function gq_guanQia_boss_gongji()

    x, y = findMultiColorInRegionFuzzy(0xfef65a, "19|1|0xfef21b,22|23|0xfef103,19|60|0xf8c201,-7|63|0x770000,35|46|0x9f0000,52|60|0xf8c413,71|57|0xf9cc09,101|49|0xfce743,192|28|0xc1c1c1,169|45|0xf8f9f8,139|28|0x890000,167|12|0xe9e2af", 80, 313, 815, 795, 995, 0, 0)
    if  x==-1 then
        x, y = findMultiColorInRegionFuzzy(0xfeef04,"14|17|0xa20000,82|22|0xfbdb13,154|16|0xfafbfb,173|22|0xc2c2c2,113|3|0x890000,-26|42|0x700000,-19|8|0xfeeb03,108|8|0x950000,52|31|0xf9cb03", 85, 315, 815, 736, 979, 0, 1)
    end
    if x > -1 then
        click(x, y)
        return true
    end
end

-- 战斗
function gq_guanQia_boss_zhanDou()

    x, y = findMultiColorInRegionFuzzy(0xfefe03, "-78|0|0xf15300,-59|-2|0xfba200,-96|68|0xefd909,-49|48|0xfdcc03,-63|70|0xf2de10,-10|66|0xfbde07,38|80|0xe1c301,44|100|0xf8f446,106|46|0xef8501,155|32|0xfefa0c,137|-13|0x2b0d00,156|-58|0xf9bd03,150|98|0xf5f215,167|92|0x1f0700", 80, 0, 0, 1079, 1919, 0, 0)
    if x > -1 then
        click(x, y)
        return true
    end
end


-- 奖励
function gq_guanQia_boss_jiangLi()

    x, y = findMultiColorInRegionFuzzy(0xfccc02, "20|-1|0xee8302,-2|20|0xf66a00,13|51|0xe56804,12|66|0xdc7e03,47|111|0xfcfb63,113|36|0xfef904,151|10|0xf35700,280|0|0xfefd15,286|26|0xfef803,265|74|0xf0a003,245|105|0xf2ed05,322|79|0xfbd806,337|77|0x200800,385|108|0xfdfd5c,427|68|0x0f0200", 80, 0, 0, 1079, 1919, 0, 0)
    if x > -1 then

        click(x, y)
        return true
    end
end


-- 奖励
function gq_guanQia_boss_zhuaBuChengDong()

    x, y = findMultiColorInRegionFuzzy(0xfeec00, "-56|26|0xfed400,-58|49|0xfeb800,-56|69|0xfea100,-31|58|0xfead00,-16|60|0xfeab00,7|69|0xfea100,51|35|0xfecb00,109|44|0xfebe00,131|58|0xfead00,248|25|0xfed500,287|24|0xfed800,303|54|0xfeb200,13|33|0x010202,220|33|0x020101", 80, 0, 0, 1079, 1919, 0, 0)
    if x > -1 then

        click(x, y)
        return true
    end
end






-- boss_继续
function gq_guanQia_boss_jixu()

    x, y = findMultiColorInRegionFuzzy(0x515ba2, "-139|77|0x1a3654,-217|53|0x515ba3,309|42|0x193553,-424|601|0x000000,604|577|0x000000,90|-131|0x000000,16|1563|0x000000", 80, 0, 0, 1079, 1919, 0, 0)
    if x > -1 then

        click(x, y)
        return true
    end
end



-- 开始战斗
function gq_kaiShiZhanDou()
    x, y = findMultiColorInRegionFuzzy(0x542011, "0|-75|0x281310,80|5|0x22110d,41|38|0xfad60d,-29|27|0xfde910,-55|22|0xfef54f,-66|-6|0x41130d", 86, 241, 851, 782, 1284, 0, 0)
    if x > -1 then
        click(x, y)
        logger("开始战斗", LOG_DEBUG)
        return true
    end
end

-- 战胜
function gq_zhanShen()

    local x, y = findMultiColorInRegionFuzzy(0xfcfb8c, "-41|54|0xfddd07,-95|70|0xf3ea0d,-71|-1|0xf05b04,43|92|0xfbfa6e,102|71|0xe7dd02,108|15|0xfd9202,143|2|0xfcad02,140|74|0xece404,204|85|0xf7f427,132|2|0x2b0c00,-18|49|0x2b0c00,114|84|0x120200", 80, 0, 0, 1079, 1919, 0, 0)
    if x > -1 then
        click(x, y)
        logger("呀!战胜了 ", LOG_DEBUG)
        return true
    end
end


-- 战败
-- isClose true不进入资产 false 进入资产
function gq_zhanBai(isClose)
    x, y = findMultiColorInRegionFuzzy(0xc1682d, "65|-3|0xcb7630,170|-4|0xcd7931,232|-4|0xcd7931,206|27|0xac5822,227|57|0x884e1e,4|57|0x874916,150|36|0x9a4a1a,30|24|0xac5720,229|172|0x010102,229|172|0x010102,-291|183|0x010101,545|188|0x010101", 80, 45, 1339, 1078, 1887, 0, 0)
    if x > -1 then

        if not (isClose) then
            click(x, y)
        end

        logger("呀!打不过了", LOG_DEBUG)
        return true
    end

end
-- 退出战斗

function gq_tuiChuZhanDou()
    x, y = findMultiColorInRegionFuzzy(0xfee34b, "21|-17|0xfee34b,13|17|0xfee34d,-26|22|0xfee44f,-14|-6|0xfee34b,0|-22|0x1e2b43,28|4|0x1e2b43,-6|33|0x1e2b43,-27|5|0x1e2b43", 86, 870, 160, 1079, 359, 0, 0)
    if x > -1 then
        return true
    end
end

-- 兵力不足
function gq_BingLiBuZhu()
    x, y = findMultiColorInRegionFuzzy(0xfcf53e, "-2|17|0xecdb57,-17|39|0xfea510,16|39|0xfeb127,147|9|0xfde949,191|19|0xfdd72a,187|336|0xba6126,108|384|0x522a14,57|322|0xcb7630,-28|343|0xac5720", 80, 141, 1329, 1001, 1871, 0, 0)
    if x > -1 then
        click(500, 500)
        return true
    end

end

function gq_boosUiGB()

    x, y = findMultiColorInRegionFuzzy(0xf0441d,"-3|-11|0xf47614,-48|1|0xfee58c,-56|-4|0x310a00,-62|6|0xfee281,-63|-16|0xfeefba,-51|-20|0xfef2c6,-90|5|0xfee385,-106|-2|0xfee694,-98|-11|0xfeecaa,-116|-12|0x310a00,-116|-18|0x310a00,-126|-4|0x310a00,-97|-2|0x310a00,-121|1|0xfee58c,-62|-16|0xfeefba", 85, 789, 23, 1079, 143, 0, 0)

    if x > -1 then
        click(x, y)
        logger("boss界面关闭", LOG_DEBUG)
        return true
    end
end
function gq_boosXuanZheMenKe()
    x, y = findMultiColorInRegionFuzzy(0xf4f1aa,"-23|9|0xf1eea8,36|5|0xf3f0aa,-33|3|0xf3f0aa,-10|15|0xf0eda8,54|0|0xf4f1aa,85|5|0xf3f0aa,106|6|0xf2efa9,127|-3|0xf4f1aa,122|2|0xf3f0aa,-23|9|0xf1eea8,0|14|0xf0eda8,5|14|0x191914,36|29|0x191913,91|9|0x1a1a15,-18|2|0x191913,-39|-2|0x171711", 85, 313, 1114, 758, 1484, 0, 0)
    if x > -1 then
        logger("boss 打不过了", LOG_DEBUG)
        return true
    end
end


function gq_allClose()
    gq_guanQia_GB()
    wangLuoLianJieShiBai()
    gq_boosUiGB()
    click_kong();
end

function gq_fanHuiUi()
    wangLuoLianJieShiBai()
    flag = gq_ui()
    if flag then
        return
    end
    flag = false;
    local index = 1
    while not (flag) do
        logger(string.format("第%s次尝试返回关卡", index), LOG_DEBUG)
        gq_allClose()
        flag = gq_ui()
        index = index + 1
    end


end


-- isClose true 则退出战斗，false 失败后进入资产

-- return  boolean1,boolean2,boolean1:是否运行成功,boolean2=是否需要关闭
function gq_zhanDou_run( isClose)
    local flag = gq_findGQ()

    if not (flag) then
        return false, false;
    end

    logger("进入关卡1", LOG_INFO)
    while flag do
        --logger("剧情继续", LOG_DEBUG)
        flag = gq_guanQia_JiXu()
        gq_findGQ()
        mSleep(500)
        wangLuoLianJieShiBai()

    end

    flag = true
    while flag do
        sysLog("战斗循环")
        gq_kaiShiZhanDou()
        sysLog("1")
        local tempFlag = gq_zhanBai(isClose)
        if tempFlag and isClose then
            sysLog("2")
            click(1025, 740)
            mSleep(1500)
            gq_guanQia_GB()
            return true, isClose
        end

        if tempFlag then
            fz_only_ziChan()
        end

        sysLog("4")
        gq_zhanShen()


        mSleep(50)
        gq_guanQia_boss_gongji()
        mSleep(50)
        gq_guanQia_boss_jiangLi()
        mSleep(50)
        gq_guanQia_boss_zhanDou()
        mSleep(50)
        gq_guanQia_boss_zhuaBuChengDong()

        mSleep(50)
        gq_guanQia_boss_jixu()
        mSleep(50)
        wangLuoLianJieShiBai()

        -- 判断是否打不过了,然后关闭游戏
        local temp=gq_boosXuanZheMenKe()
        if temp then
            gq_boosUiGB()
            return true, isClose
        end

        -- 检测是否还要继续
        flag = gq_ui()
        flag = not (flag)
        sysLog("5")
        wangLuoLianJieShiBai()

    end
    sysLog("6")
    return true, false
end


function gq_boss_run(isClose)
    local flag = gq_findGQ_boss()

    if not (flag) then
        return false,false;
    end

    logger("进入关卡2", LOG_INFO)
    while flag do
        --logger("剧情继续", LOG_DEBUG)
        flag = gq_guanQia_JiXu()
        mSleep(500)
        wangLuoLianJieShiBai()

    end

    flag = false;
    while not (flag) do

        --- 判断是否已退出boss关卡
        flag = gq_ui()
        if flag then
            return true,isClose
        end
        mSleep(100)
        gq_guanQia_boss_gongji()
        mSleep(50)
        gq_guanQia_boss_jiangLi()
        mSleep(50)
        gq_guanQia_boss_zhanDou()
        mSleep(50)
        gq_guanQia_boss_zhuaBuChengDong()

        mSleep(50)
        gq_guanQia_boss_jixu()
        mSleep(50)
        wangLuoLianJieShiBai()
        
        -- 判断是否打不过了,然后关闭游戏
        local temp=gq_boosXuanZheMenKe()
        if temp then
            gq_boosUiGB()
            return true,isClose
        end
        

    end
    return true,false
end




-- result  true :需要继续,false:不需要继续
function gq_run(isClose)
    local flag = true

    mSleep(500)
    isCloseTemp = false
    --
    logger("查找可玩关卡", LOG_DEBUG)
		
    flag, isCloseTemp = gq_zhanDou_run(isClose)
    if not (flag) then
        flag,isCloseTemp = gq_boss_run(isClose)
    end
    mSleep(1000)

    if isClose and isCloseTemp then
        gq_ui_GB()
        return false
    end
    return true
end 
